package tool;

import java.util.ArrayList;

import ItemUser.MoiveRate;
import ItemUser.UserRate;

public class Similarity {
	public static float sim(ArrayList<UserRate> moive1, ArrayList<UserRate> moive2)
	{
		int size = 0;
		if(moive1.size() != moive2.size()){
			 size = moive1.size()>moive2.size()?moive1.size():moive2.size();
		}else{
			size = moive1.size();
		}
		
		float simVal = 0;
	
		float num = 0;
		float den = 1;
		float a = 0;
		float b = 0;
		float rate1 = 0;
		float rate2 = 0;
		for(int i = 0;i < size;i++){
			
			if(i < moive1.size() && moive1.get(i) != null){
				rate1 = moive1.get(i).getRating();
				a = (float) Math.pow(moive1.get(i).getRating(),2) + a;
			}else{
				rate1 = 0;
				a = 0 + a;
			}
			if(i < moive2.size() && moive2.get(i) != null){
				rate2 = moive2.get(i).getRating();
				b = (float) Math.pow(moive2.get(i).getRating(),2) + b;
			}else{
				rate2 = 0;
				b = 0 + b;
			}
			num = rate1*rate2+num;
			
		}
		den = (float) (Math.sqrt(a)* Math.sqrt(b));
		
		simVal = num / den;
		return simVal;
	}
}
